import matplotlib.pyplot as plt
import sys


def DrawProcess(shape, lock, subs_list, fig_title):
    flag = True
    fig = plt.figure(figsize=(10, 10))

    def build_subplot(subs_list):
        plt.clf()
        lock.acquire()
        subs = [None] * len(subs_list)
        for i, sub in enumerate(subs_list):
            subs[i] = fig.add_subplot(shape + str(i+1))
            subs[i].set_title(sub["title"])
            subs[i].set_xlabel(sub["xlabel"])
            subs[i].set_ylabel(sub["ylabel"])
            subs[i].plot(sub["data"], **sub["style"])

        plt.suptitle(fig_title, fontsize=15)
        lock.release()
        try:
            plt.pause(1)
        except Exception as e:
            sys.exit()

    def run():
        try:
            build_subplot(subs_list)
        except Exception as e:
            sys.exit()

    while flag:
        if fig is None:
            break
        run()

def save_fig(shape, subs_list, fig_title, path):
    fig = plt.figure(figsize=(10, 10))
    subs = [None] * len(subs_list)
    for i, sub in enumerate(subs_list):
        subs[i] = fig.add_subplot(shape + str(i+1))
        subs[i].set_title(sub["title"])
        subs[i].set_xlabel(sub["xlabel"])
        subs[i].set_ylabel(sub["ylabel"])
        subs[i].plot(sub["data"], **sub["style"])

    plt.suptitle(fig_title, fontsize=15)
    plt.savefig(path)